package com.hyt.it.ogt.kq.service.gov.service.impl;

import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.kq.service.gov.mapper.OfficeTaskConfigMapper;
import com.hyt.it.ogt.kq.service.gov.model.entity.OfficeTaskConfig;
import com.hyt.it.ogt.kq.service.gov.model.param.TaskOffLineConfig;
import com.hyt.it.ogt.kq.service.gov.model.param.TaskOnLineConfig;
import com.hyt.it.ogt.kq.service.gov.service.IOfficeTaskConfigService;

import cn.hutool.core.collection.CollUtil;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 任务行为分析模板表 服务实现类
 * </p>
 *
 * @author huangyh
 * @since 2021-08-19
 */
@Service
public class OfficeTaskConfigServiceImpl extends BaseServiceImpl<OfficeTaskConfigMapper, OfficeTaskConfig> implements IOfficeTaskConfigService {

    @Resource
    private OfficeTaskConfigMapper officeTaskConfigMapper;

    @Override
    public List<OfficeTaskConfig> queryOfficeTaskConfigView(String officeId) {

        List<OfficeTaskConfig> officeTaskConfigList = this.lambdaQuery()
                .eq(OfficeTaskConfig::getOfficeId,officeId)
                .eq(OfficeTaskConfig::getIsView,true)
                .eq(OfficeTaskConfig::getDelFlag,false)
                .list();

        if (CollUtil.isEmpty(officeTaskConfigList)) {
            //初始化参数
            initOfficeTaskConfig(officeId);

            officeTaskConfigList = this.lambdaQuery()
                    .eq(OfficeTaskConfig::getOfficeId,officeId)
                    .eq(OfficeTaskConfig::getIsView,true)
                    .eq(OfficeTaskConfig::getDelFlag,false)
                    .list();
        }

        return officeTaskConfigList;
    }

    @Override
    public List<TaskOffLineConfig> getTaskOffLineConfigByOfficeId(String officeId) {
        List<OfficeTaskConfig> officeTaskConfigs = queryOfficeTaskConfigView(officeId);

        List<TaskOffLineConfig> taskOffLineConfigs = new ArrayList<>();
        officeTaskConfigs.stream().forEach(OfficeTaskConfig -> {
            TaskOffLineConfig taskOffLineConfig = new TaskOffLineConfig();
            BeanUtils.copyProperties(OfficeTaskConfig, taskOffLineConfig);
            taskOffLineConfig.setConfigValue(OfficeTaskConfig.getValue()).setConfigCode(OfficeTaskConfig.getCode());
            taskOffLineConfigs.add(taskOffLineConfig);
        });

        // 需要过滤掉相同configCode的数据
        Map<String, TaskOffLineConfig> coonfigCodeMap = new HashMap<>();
        for (Iterator<TaskOffLineConfig> iterator = taskOffLineConfigs.iterator(); iterator.hasNext();) {
            TaskOffLineConfig pushExamConfigParamTemp = iterator.next();
            if (coonfigCodeMap.containsKey(pushExamConfigParamTemp.getConfigCode())) {
                iterator.remove();
            }
            coonfigCodeMap.put(pushExamConfigParamTemp.getConfigCode(), pushExamConfigParamTemp);
        }
        return taskOffLineConfigs;
    }


    /**
     * 初始化机构任务配置参数
     * @param officeId
     */
    @Override
    public void initOfficeTaskConfig(String officeId) {
        Integer count = this.lambdaQuery()
                .eq(OfficeTaskConfig::getOfficeId,officeId)
                .eq(OfficeTaskConfig::getDelFlag,false)
                .count();
        if (count <= 0) {
            officeTaskConfigMapper.initOfficeTaskConfig(officeId);
        }
    }

}
